//
// Created on 2025/6/3.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include "include/effects/SkImageFilters.h"
#include "src/core/SkReadBuffer.h"

#include "oh/OHDrawingAPI.h"
#include "src/core/SkImageFilter_Base.h"
#include <native_drawing/drawing_image_filter.h>

class SkColorFilterOffset final : public SkImageFilter_Base {
public:
    SkColorFilterOffset(SkScalar dx, SkScalar dy, sk_sp<SkImageFilter> input, const SkImageFilters::CropRect &cropRect)
            : SkImageFilter_Base(&input, 1, &cropRect.fCropRect) {
        fOHImageFilter = OHDrawingAPI::OH_Drawing_ImageFilterCreateOffset(dx, dy, input ? *as_IFB(input) : nullptr);
        fX = dx;
        fY = dy;
        DrawingHash hasher(HASH_UNIQUE_ID);
        hasher.push(fX);
        hasher.push(fY);
        if (input) {
            hasher.push(as_IFB(input)->hash());
        }
        fHash = hasher.digest();
    }
    ~SkColorFilterOffset() {
        if (fOHImageFilter) {
            OH_Drawing_ImageFilterDestroy(fOHImageFilter);
        }
    }
    
    SkRect computeFastBounds(const SkRect&) const override;
    
    float computeFastDrawCost() const override;
    
private:
    SK_FLATTENABLE_HOOKS(SkColorFilterOffset)
    
    SkScalar fX = 0;
    SkScalar fY = 0;
};

sk_sp<SkFlattenable> SkColorFilterOffset::CreateProc(SkReadBuffer& buffer) {
    SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
    sk_sp<SkColorFilter> cf(buffer.readColorFilter());
    return SkImageFilters::ColorFilter(std::move(cf), common.getInput(0), common.cropRect());
}


sk_sp<SkImageFilter> SkImageFilters::Offset(SkScalar dx, SkScalar dy, sk_sp<SkImageFilter> input,
                                            const CropRect &cropRect) {
    return sk_sp<SkImageFilter>(new SkColorFilterOffset(dx, dy, input, cropRect));
}


SkRect SkColorFilterOffset::computeFastBounds(const SkRect& src) const {
    SkRect bounds = this->getInput(0) ? this->getInput(0)->computeFastBounds(src) : src;
    SkMatrix m = SkMatrix::Translate(fX, fY);
    return m.mapRect(bounds);
}

float SkColorFilterOffset::computeFastDrawCost() const {
    return this->getInput(0) ? this->getInput(0)->computeFastDrawCost() : 0.f;
}
